package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.EmployeeService;
import com.itheima.reggie.domain.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

//表示层 把他放入spring域中
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @Autowired
    private HttpSession httpSession;
    //登录
    @PostMapping("/employee/login")
    public ResultInfo login(@RequestBody Map<String ,String >map){
        //接受请求参数
        String username = map.get("username");
        String password = map.get("password");
        System.out.println("password = " + password);
        //调用service登录
       ResultInfo employee= employeeService.login(username,password);
       // 登录成功，放入到session域
        Employee data = (Employee) employee.getData();
        httpSession.setAttribute("SESSION_EMPLOYEE",data);
        // 结果返回
        System.out.println("pppppppppppppppppp");
        return employee;
    }

    //登录退出
    @PostMapping("/employee/logout")
    public ResultInfo logout(){
        //注销用户
        httpSession.invalidate();
        //返回成功
        return ResultInfo.success(null);
    }

    //查询员工所有
    @GetMapping("/employee/find")
    public ResultInfo findByName(String name){
        List<Employee>list=employeeService.findByName(name);
        return ResultInfo.success(list);
    }

    //新增员工信息
    @PostMapping("/employee")
    public ResultInfo save(@RequestBody Employee employee){
        employeeService.save(employee);
        //返回一个成功标识
        return ResultInfo.success(null);
    }

    //根据id员工数据查询
    @GetMapping("/employee/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
       Employee employee= employeeService.findById(id);
        return ResultInfo.success(employee);
    }

    //修改员工参数
    @PutMapping("/employee")
    public ResultInfo update(@RequestBody Employee employee){
        employeeService.update(employee);
        return ResultInfo.success(null);
    }
}
